package com.wuli.food.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuli.food.common.apiModel.R;
import com.wuli.food.common.utils.StringUtils;
import com.wuli.food.dto.TypeDto;
import com.wuli.food.entity.Food;
import com.wuli.food.service.FoodService;
import com.wuli.food.service.FoodTypeService;
import com.wuli.food.vo.TypeAddVo;
import com.wuli.food.vo.TypeSearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiongqiaoqiao
 * @since 2023-05-18
 */
@Api(tags = "菜品类型管理接口")
@RestController
@RequestMapping("/type")
public class FoodTypeController {
    @Autowired
    private FoodTypeService typeService;
    @Autowired
    private FoodService foodService;

    @PostMapping("/search")
    @ApiOperation("分页条件查询")
    public R<?> search(@RequestBody TypeSearchVo searchVo) {
        IPage<TypeDto> list = typeService.search(searchVo);
        return new R<>().Success(list);
    }

    @ApiOperation("新增和修改")
    @PostMapping("/add")
    public R add(@RequestBody TypeAddVo addVo){
        String id = typeService.add(addVo);
        return new R<>().Success(id);
    }

    @ApiOperation("查看详情")
    @GetMapping("/getById")
    public R getDetail(@ApiParam("菜品Id") @NotBlank(message = "id不能为空") String id){
        TypeDto dot = typeService.getDetail(id);
        return new R<>().Success(dot);
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public R delete(@ApiParam("类型Id") @NotBlank(message = "id不能为空") String id){
        LambdaQueryWrapper<Food> qw = new LambdaQueryWrapper<>();
        qw.eq(Food::getTypeId, id);
        List<Food> list = foodService.list(qw);
        if (list.size() != 0){
            return new R<>().Error("该套餐关联菜品，不能删除！");
        }
        typeService.deleteById(id);
        return new R<>().Success("删除成功！");
    }
}

